package com.jdew.thkj.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.jdew.thkj.R;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.piechart.PieChart;
import com.jdew.thkj.piechart.PieDataEntity;
import com.jdew.thkj.utils.DateTimeUtils;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.WeiboDialogUtils;
import com.jdew.thkj.weiget.CustomDatePicker;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

import butterknife.Bind;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangzai on 2018/6/6.
 */

public class CommissionsActivity extends BaseActivity {
    @Bind(R.id.chart)
    PieChart piechart;
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.yestoday)
    TextView yestoday;
    @Bind(R.id.seven)
    TextView seven;
    @Bind(R.id.themonth)
    TextView themonth;
    @Bind(R.id.lastmonth)
    TextView lastmonth;
    @Bind(R.id.person_yj)
    TextView personYj;
    @Bind(R.id.team_yj)
    TextView teamYj;
    @Bind(R.id.teammanager_yj)
    TextView teammanagerYj;
    @Bind(R.id.persent1)
    TextView persent1;
    @Bind(R.id.persent2)
    TextView persent2;
    @Bind(R.id.persent3)
    TextView persent3;
    @Bind(R.id.selectTime)
    TextView selectTime;
    @Bind(R.id.select)
    TextView select;
    private CustomDatePicker customDatePicker;
    private SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat matter2 = new SimpleDateFormat("yyyy-MM");
    private Date nowdate;
    private String date;
    public SharedPreferences sp;
    public SharedPreferences.Editor editor;
    private int[] mColors = {0xFFFC9E1A, 0xFF2DB4E5, 0xFFFB7FBB};
    private List<PieDataEntity> dataEntities = new ArrayList<>();
    private Dialog mWeiboDialog;

    @Override
    public int bindLayout() {
        return R.layout.activity_commissions;
    }

    @Override
    public void initView(View view) {
        back.setVisibility(View.VISIBLE);
        title.setText("佣金明细");
        back.setOnClickListener(this);
        yestoday.setOnClickListener(this);
        seven.setOnClickListener(this);
        themonth.setOnClickListener(this);
        lastmonth.setOnClickListener(this);
        select.setOnClickListener(this);
        sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        date = matter1.format(DateTimeUtils.getYestoday(new Date()));
        selectTime.setText(date);
        GetTimeMonthMoney(date, "0");
        initDate();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            case R.id.yestoday: {
                String nowTime = matter1.format(new Date());
                Date date = DateTimeUtils.string2Date(nowTime);
                Date dt = DateTimeUtils.getYestoday(date);
                yestoday.setTextColor(getResources().getColor(R.color.colorAccent));
                seven.setTextColor(getResources().getColor(R.color.top_title));
                themonth.setTextColor(getResources().getColor(R.color.top_title));
                lastmonth.setTextColor(getResources().getColor(R.color.top_title));
                selectTime.setText(matter1.format(dt));
                GetTimeMonthMoney(matter1.format(dt), "0");
            }
            break;
            case R.id.seven: {
                String nowTime = matter1.format(new Date());
                Date date = DateTimeUtils.string2Date(nowTime);
                Date dt = DateTimeUtils.get7dayago(date);
                yestoday.setTextColor(getResources().getColor(R.color.top_title));
                seven.setTextColor(getResources().getColor(R.color.colorAccent));
                themonth.setTextColor(getResources().getColor(R.color.top_title));
                lastmonth.setTextColor(getResources().getColor(R.color.top_title));
                selectTime.setText(DateTimeUtils.getWeekMonday() + "—" + matter1.format(new Date()));
                GetTimeMonthMoney(matter1.format(dt), "7");
            }
            break;
            case R.id.themonth: {
                yestoday.setTextColor(getResources().getColor(R.color.top_title));
                seven.setTextColor(getResources().getColor(R.color.top_title));
                themonth.setTextColor(getResources().getColor(R.color.colorAccent));
                lastmonth.setTextColor(getResources().getColor(R.color.top_title));
                date = matter2.format(new Date());
                selectTime.setText(DateTimeUtils.getTimeOfMonthStart() + "—" + matter1.format(nowdate));
                GetTimeMonthMoney(date, "0");
            }
            break;
            case R.id.lastmonth: {
                Calendar ca = Calendar.getInstance();
                ca.setTime(new Date());
                ca.add(Calendar.MONTH, -1);
                Date dt = ca.getTime();
                yestoday.setTextColor(getResources().getColor(R.color.top_title));
                seven.setTextColor(getResources().getColor(R.color.top_title));
                themonth.setTextColor(getResources().getColor(R.color.top_title));
                lastmonth.setTextColor(getResources().getColor(R.color.colorAccent));
                selectTime.setText(DateTimeUtils.getTimeOfLastMonthStart() + "—" + DateTimeUtils.getTimeOfLastMonthEnd());
                GetTimeMonthMoney(matter2.format(dt), "0");
            }
            break;
            case R.id.select: {
                customDatePicker.show(matter1.format(nowdate));
            }
            break;
            default:
                break;
        }
    }

    public void initDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        final Calendar calendar = Calendar.getInstance();
        nowdate = new Date(System.currentTimeMillis());
        date = sdf.format(DateTimeUtils.getLast2Month(nowdate));
        String now = sdf.format(new Date());
        customDatePicker = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) {
                // 回调接口，获得选中的时间
                selectTime.setText(time.split(" ")[0]);
                yestoday.setTextColor(getResources().getColor(R.color.top_title));
                seven.setTextColor(getResources().getColor(R.color.top_title));
                themonth.setTextColor(getResources().getColor(R.color.top_title));
                lastmonth.setTextColor(getResources().getColor(R.color.top_title));
                GetTimeMonthMoney(selectTime.getText().toString(), "0");
            }
        }, date, now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker.showSpecificTime(false); // 不显示时和分
        customDatePicker.setIsLoop(false); // 允许循环滚动
    }

    public void GetTimeMonthMoney(String leftTime, String rightTime) {
        mWeiboDialog = WeiboDialogUtils.createLoadingDialog(this);
        String timesamp= RegexValidateUtils.getTime13();
        String numsamp=RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).GetTimeMonthMoney(sp.getString("mpid", ""), sp.getString("userPhone", ""), leftTime, rightTime, RegexValidateUtils.getUniquePsuedoID(),timesamp,numsamp,MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp+numsamp+RegexValidateUtils.getUniquePsuedoID()+sp.getString("SignToken","")+"Pid"+sp.getString("mpid", "")+"phone"+sp.getString("userPhone", "")+"LeftTime"+leftTime+"RightTime"+rightTime)).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {
                WeiboDialogUtils.closeDialog(mWeiboDialog);
            }

            @Override
            public void onError(Throwable e) {
                WeiboDialogUtils.closeDialog(mWeiboDialog);
            }

            @Override
            public void onNext(JSONObject obj) {
                WeiboDialogUtils.closeDialog(mWeiboDialog);
                if (obj.getString("code").equals("0")) {
                    dataEntities.clear();
                    String person = obj.getString("data");
                    String team = obj.getString("num");
                    String teammaneger = obj.getString("numto");
                    personYj.setText("¥ " + person);
                    teamYj.setText("¥ " + team);
                    teammanagerYj.setText("¥ " + teammaneger);
                    String total = new DecimalFormat("0.00").format(Double.parseDouble(person) + Double.parseDouble(team) + Double.parseDouble(teammaneger));
                    String p1, p2, p3;
                    if (!total.equals("0.00")) {
                        p1 = new DecimalFormat("0.00").format(Double.parseDouble(person) / Double.parseDouble(total) * 100);
                        p2 = new DecimalFormat("0.00").format(Double.parseDouble(team) / Double.parseDouble(total) * 100);
                        p3 = new DecimalFormat("0.00").format(Double.parseDouble(teammaneger) / Double.parseDouble(total) * 100);
                        persent1.setText(p1 + "%");
                        persent2.setText(p2 + "%");
                        persent3.setText(p3 + "%");
                        PieDataEntity entity = new PieDataEntity("" + 1, Float.parseFloat(person), mColors[0]);
                        dataEntities.add(entity);
                        PieDataEntity entity1 = new PieDataEntity("" + 2, Float.parseFloat(team), mColors[1]);
                        dataEntities.add(entity1);
                        PieDataEntity entity2 = new PieDataEntity("" + 3, Float.parseFloat(teammaneger), mColors[2]);
                        dataEntities.add(entity2);
                        piechart.setDataList(dataEntities);
                        piechart.startAnimation(1000);
                    } else {
                        persent1.setText("0.00%");
                        persent2.setText("0.00%");
                        persent3.setText("0.00%");
                        PieDataEntity entity = new PieDataEntity("" + 1, 1.11111f, mColors[0]);
                        dataEntities.add(entity);
                        PieDataEntity entity1 = new PieDataEntity("" + 2, 1.11111f, mColors[1]);
                        dataEntities.add(entity1);
                        PieDataEntity entity2 = new PieDataEntity("" + 3, 1.11111f, mColors[2]);
                        dataEntities.add(entity2);
                        piechart.setDataList(dataEntities);
                        piechart.startAnimation(1000);
                    }
                }
            }
        });
    }

}
